#include "tabbarpanel.h"
#include "toptabbar.h"
#include "tabbarhorline.h"
#include <QPushButton>
#include <QPainter>
#include <QMatrix>
#include <QResizeEvent>
#include <QPixmap>
#include <QtDebug>
#include <QMenu>

using namespace ExecTerminalImplNS;

const int PANEL_HEIGHT = 35;
const int BUTTONS_LEFT_INDENT = 14;
const int BUTTONS_INDENT_FROM_HOR_LINE = 1;
const int BUTTONS_SPACING = 2;
const int INDENT_FROM_BUTTONS_TO_TABBAR = 9;
const int TABBAR_RIGHT_INDENT = 15;
const int TABBAR_INDENT_FROM_HOR_LINE = -2;
const int BUTTON_WIDTH = 19;
const int BUTTON_HEIGHT = 19;
const int HOR_LINE_BEGIN = -1;

TabBarPanel::TabBarPanel(QWidget * parent): QWidget(parent),
theTabBar(0),
theTabBarLine(0),
theListButton(0),
theThumbnailsButton(0),
theNewTabButton(0),
theTabBarLeftIndent(0),
theTabUID(0)
{
    resize(width(), PANEL_HEIGHT);
    theTabBar = new TopTabBar(this);
    theTabBarLine = new TabBarHorLine(this);

    theListButton = new QPushButton(this);
    theListButton->setObjectName("listButton");
    theListButton->setFocusPolicy(Qt::NoFocus);

    theThumbnailsButton = new QPushButton(this);
    theThumbnailsButton->setObjectName("thumbnailsButton");
    theThumbnailsButton->setFocusPolicy(Qt::NoFocus);

    theNewTabButton = new QPushButton(this);
    theNewTabButton->setObjectName("newTabButton");
    theNewTabButton->setFocusPolicy(Qt::NoFocus);
    connect(theNewTabButton, SIGNAL(clicked()), SLOT(addTab()));

    theTabBar->raise();
    layoutButtons();
    layoutTabBar();
}

TopTabBar * TabBarPanel::tabBar()const
{
    return theTabBar;
}

QPushButton * TabBarPanel::listButton()const
{
    return theListButton;
}

QPushButton * TabBarPanel::thumbnailsButton()const
{
    return theThumbnailsButton;
}

QPushButton * TabBarPanel::newTabButton()const
{
    return theNewTabButton;
}

void TabBarPanel::setOrientation(Orientation ornt)
{
    if (ornt != theTabBar->orientation())
    {
        theTabBar->setOrientation(static_cast<Orientation>(ornt));
        layoutButtons();
        update();
    }
}

void TabBarPanel::changeOrientation()
{
    if (orientation() == DOWN_ORIENTATION)
    {
        setOrientation(UP_ORIENTATION);
    }
    else
    {
        setOrientation(DOWN_ORIENTATION);
    }
}

void TabBarPanel::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    const bool isUpOrientation = orientation() == UP_ORIENTATION;
    int bgrndY = 0;
    if (!isUpOrientation)
    {
        QMatrix rotMatrix;
        rotMatrix.translate(width(), height() - theTabBarLine->height());
        rotMatrix.rotate(180);
        painter.setMatrix(rotMatrix);
        bgrndY = -theTabBarLine->height();
    }
    static QPixmap backgrnd (":/Resources/top_bg_gradient_2.png");
    painter.drawTiledPixmap(0, bgrndY, width(), height() - theTabBarLine->height(), backgrnd);
    if (theTabBar->visualInsertPos() >= 0)
    {
        static QPixmap insertArrow(":/Resources/bookmarks_insert_arrow.png");
        const int xPos = theTabBar->visualInsertPos() + theTabBar->x() 
            - insertArrow.width() / 2 - 1;
        const int yPos = theTabBar->y() - insertArrow.height() + 1;
        painter.drawPixmap(xPos,  yPos, insertArrow);
    }

}

void TabBarPanel::resizeEvent(QResizeEvent * e)
{
    if (e->oldSize().height() != e->size().height())
    {
        layoutButtons();
    }
    layoutTabBar();
    update();
}

void TabBarPanel::layoutButtons()
{
    setUpdatesEnabled(false);
    int x = BUTTONS_LEFT_INDENT;
    const bool isUpOrientation = theTabBar->orientation() == UP_ORIENTATION;

    const int horLineYPos = isUpOrientation ? height() - theTabBarLine->height() : 0; 
    const int buttonYPos = isUpOrientation ? horLineYPos - BUTTONS_INDENT_FROM_HOR_LINE - BUTTON_HEIGHT 
        : horLineYPos + theTabBarLine->height() + BUTTONS_INDENT_FROM_HOR_LINE;
    
    theListButton->setGeometry(x, buttonYPos, BUTTON_WIDTH, BUTTON_HEIGHT);
    x += BUTTON_WIDTH + BUTTONS_SPACING;
    theThumbnailsButton->setGeometry(x, buttonYPos, BUTTON_WIDTH, BUTTON_HEIGHT);
    x += BUTTON_WIDTH + BUTTONS_SPACING;
    theNewTabButton->setGeometry(x, buttonYPos, BUTTON_WIDTH, BUTTON_HEIGHT);
    x += BUTTON_WIDTH + INDENT_FROM_BUTTONS_TO_TABBAR;

    theTabBarLeftIndent = x;
    const int tabBarYPos = isUpOrientation ? horLineYPos - theTabBar->height() - TABBAR_INDENT_FROM_HOR_LINE 
        : horLineYPos + theTabBarLine->height() + TABBAR_INDENT_FROM_HOR_LINE;

    theTabBar->move(theTabBarLeftIndent, tabBarYPos);
    theTabBarLine->move(HOR_LINE_BEGIN, horLineYPos);
    setUpdatesEnabled(true);
}

void TabBarPanel::layoutTabBar()
{
    setUpdatesEnabled(false);
    theTabBar->resize(width() - theTabBarLeftIndent - TABBAR_RIGHT_INDENT, theTabBar->height());
    theTabBarLine->resize(width() + 1, theTabBarLine->height());
    setUpdatesEnabled(true);
}

TabBarHorLine * TabBarPanel::tabBarLine()const
{
    return theTabBarLine;
}


void TabBarPanel::addTab()
{
    static int i;
    int tabNumber = 
        theTabBar->addTab(QPixmap(":/Resources/ico_dark.png"), tr("Empty page %1").arg(i++));
    theTabBar->setTabData(tabNumber, theTabUID);
    emit tabCreated(tabNumber, theTabUID);
    theTabUID++;

    theListButton->setMenu(theTabBar->tabsMenu());
}

QSize TabBarPanel::minimumSizeHint()const
{
    const int tabBarMinWidth = theTabBar->minimumSizeHint().width();   
    return QSize(theTabBarLeftIndent + tabBarMinWidth + TABBAR_RIGHT_INDENT,
        PANEL_HEIGHT);
}


Orientation TabBarPanel::orientation()const
{
    return theTabBar->orientation();
}


